<template>
	<view class="ys">
		<view class="form">
			<uni-section title="类型" type="line">
				<view class="inputbox">
					<uni-data-select v-model="type" :localdata="typeRange"></uni-data-select>
				</view>
			</uni-section>
			<uni-section title="房间订单" type="line">
				<view class="inputbox">
					<uni-data-select v-model="order_id" :localdata="orderRange" @change="changeOrder"></uni-data-select>
				</view>
			</uni-section>
			<uni-section title="派单金额" type="line">
				<view class="inputbox">
					<uni-easyinput v-model="money" trim placeholder="请输入金额" clearSize="20"></uni-easyinput>
				</view>
			</uni-section>
		</view>

		<button class="btn" @click="serviceCharge" :loading="loading" :disabled="loading">提交</button>
	</view>
</template>

<script>
	import {
		getChargeType,
		getFinishRoomOrder,
		serviceCharge
	} from "@/api/user.js"
	export default {
		data() {
			return {
				type: '',
				typeRange: [],
				order_id: '',
				orderRange: [],
				orderList: [],
				money: '',
				loading: false,
			}
		},
		onLoad() {
			this.getChargeType()
			this.getFinishRoomOrder()
		},
		methods: {
			getFinishRoomOrder() {
				getFinishRoomOrder().then(res => {
					if (res.code == 0) {
						let data = res.data
						this.orderList = data
						this.orderRange = data.map(function(item) {
							return {
								text: `${item.room.name},完成:${item.finish_time}`,
								value: item.id
							}
						})
					}
				})
			},
			getChargeType() {
				getChargeType().then(res => {
					if (res.code == 0) {
						let arr = Object.keys(res.data)
						let list = Object.values(res.data)
						arr.forEach((item, index) => {
							this.typeRange.push({
								value: item,
								text: list[index]
							})
						})
					}
				})
			},
			changeOrder(e) {

			},
			serviceCharge() {
				let that = this
				if (this.type == '') {
					return uni.$showMsg('请选择类型')
				}
				if (this.order_id == '') {
					return uni.$showMsg('请选择房间')
				}
				if (this.money == '') {
					return uni.$showMsg('请输入金额')
				}
				that.loading = true
				serviceCharge({
					type: that.type,
					order_id: that.order_id,
					money: that.money
				}).then(res => {
					uni.$showMsg(res.msg)
					if (res.code == 0) {
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					} else {
						that.loading = false
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.ys {
		.inputbox {
			width: 96%;
			margin: 0px auto;
		}

		.form {
			padding-bottom: 15px;
			background: #fff;
		}

		.btn {
			button {
				color: #fff;
				font-size: 16px;
				letter-spacing: 1px;
				margin: 30px auto;
				width: 96%;
			}
		}
	}
</style>